﻿// SilverlightDesktop - http://www.SilverlightDesktop.net
// Copyright (c) 2008-2009
// by SilverlightDesktop
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.Linq;
using System.Linq;
using System.Text;
using System.Web;
using System.Xml.Linq;

using DotNetNuke;
using DotNetNuke.Security;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Localization;

namespace SilverlightDesktop.Module
{
    public partial class View : DotNetNuke.Entities.Modules.PortalModuleBase, DotNetNuke.Entities.Modules.IActionable
    {
        string SDAutoLoad;
        public string SilverlightInitParams { get; set; }
        public string SilverlightSourceParams { get; set; }
        public string SilverlightHeightParams { get; set; }
        public string SilverlightWidthParams { get; set; }
        public string SilverlightWindowlessParams { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
                // Register Silverlight.js file
                Page.ClientScript.RegisterClientScriptInclude(this.GetType(), "SilverlightJS",
                    (this.TemplateSourceDirectory + "/Silverlight.js"));

                LoadDesktopInstance();

                DotNetNuke.Entities.Users.UserInfo objUser;
                objUser = DotNetNuke.Entities.Users.UserController.GetCurrentUserInfo();
                int intPortalID = objUser.PortalID;
                int intUserID = objUser.UserID;

                string strIPAddress = this.Context.Request.UserHostAddress;
                string strSilverlightPassword = Authendication.SetSilverlightKey(objUser, ModuleId, strIPAddress);
                string strWebServiceBase = GetWebServiceBase();

                SilverlightSourceParams = this.TemplateSourceDirectory + "/ClientBin/SilverlightDesktop.xap";

                SilverlightInitParams =
                    String.Format("PortalID={0},ModuleId={1},UserID={2},Password={3},WebServiceBase={4},AutoLoadModule={5}",
                    "0", ModuleId.ToString(), intUserID.ToString(), strSilverlightPassword, strWebServiceBase, SDAutoLoad);
        }

        #region GetWebServiceBase
        private string GetWebServiceBase()
        {
            string strWebServiceBase = String.Format(@"{0}://", this.Request.Url.Scheme);
            strWebServiceBase = String.Format(@"{0}{1}", strWebServiceBase, this.Request.Url.Host.Replace(@"\", @"/"));
            strWebServiceBase = String.Format(@"{0}{1}", strWebServiceBase, ((this.Request.Url.Port == 80) ? "" : ":" + this.Request.Url.Port.ToString()));
            strWebServiceBase = String.Format(@"{0}{1}", strWebServiceBase, this.Request.ApplicationPath.Replace(@"\", ""));
            strWebServiceBase = String.Format(@"{0}/DesktopModules/SilverlightDesktop/webservice/", strWebServiceBase);
            return strWebServiceBase;
        }
        #endregion

        #region LoadDesktopInstance
        private void LoadDesktopInstance()
        {
            if ((Settings["SDVertical"]) == null)
            {
                SilverlightHeightParams = "500px";
                SilverlightDesktopControl.Height = 500;
            }
            else
            {
                SilverlightHeightParams = Convert.ToString((Settings["SDVertical"]));
                SilverlightDesktopControl.Height = Convert.ToInt32((Settings["SDVertical"]));
            }

            if ((Settings["SDHorizontal"]) == null)
            {
                SilverlightWidthParams = "500px";
                SilverlightDesktopControl.Width = 500;
            }
            else
            {
                SilverlightWidthParams = Convert.ToString((Settings["SDHorizontal"]));
                SilverlightDesktopControl.Width = Convert.ToInt32((Settings["SDHorizontal"]));
            }

            if ((Settings["SDTransparent"]) != null)
            {
                if (Convert.ToBoolean(Settings["SDTransparent"]) == true)
                {
                    SilverlightWindowlessParams = "<param name='windowless' value='true' /> <param name='background' value='Transparent' />";
                }
                else
                {
                    SilverlightWindowlessParams = "";
                }
            }

            if ((Settings["SDAutoLoad"]) == null)
            {
                SDAutoLoad = "0";
            }
            else
            {
                SDAutoLoad = Convert.ToString((Settings["SDAutoLoad"]));
            }
        }
        #endregion

        #region IActionable Members
        public DotNetNuke.Entities.Modules.Actions.ModuleActionCollection ModuleActions
        {
            get
            {
                DotNetNuke.Entities.Modules.Actions.ModuleActionCollection Actions = new DotNetNuke.Entities.Modules.Actions.ModuleActionCollection();
                Actions.Add(
                    GetNextActionID(),
                    "Module Administration",
                    DotNetNuke.Entities.Modules.Actions.ModuleActionType.AddContent, "", "", EditUrl(), false, SecurityAccessLevel.Edit, true, false);
                
                return Actions;
            }
        }
        #endregion
    }
}